global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/Table_A36_firmsize.log, replace name(tabl)

capture noi { 

	qui do ${code_dir}/config/tabletools.do

	use ${final_dir}/regression_dataset${weight_window}${tf}.dta, clear

	mmerge BvD using ${final_dir}/bvd_list_regfirms_auto95.dta, unmatched(master)
	keep if _m == 3
	drop _m

	clonevar LSW = lswMPm_1995_a
	clonevar HSW = hswMPm_1995_a
	clonevar VAEMP = vaempMPm_1995_a
	clonevar GDPGAP = lngdpgap_1995_a
	clonevar GDPPC = gdppcMPm_1995_a
	clonevar spilloversown = spill${depvar}${ttt}_1995_a
	clonevar spilloversother = spillN${depvar}${ttt}_1995_a
	clonevar spilloversownzero = spill${depvar}${ttt}_1995_a0
	clonevar spilloversotherzero = spillN${depvar}${ttt}_1995_a0
	clonevar stockown = k${depvar}_${ttt} 
	clonevar stockownzero = k${depvar}_${ttt}0  
	clonevar stockother = kNOT_${depvar}_${ttt} 
	clonevar stockotherzero = kNOT_${depvar}_${ttt}0
	bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995+2 & year <= 2009+2
	bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
	drop _total_${depvar}_${ttt}_1995

	* generate firmsize bins based on total stock in 1995
	gen stock_tot_1995=log(exp(stockown)+exp(stockother)) if year==1995
	su stock_tot_1995, detail
	qui sum stock_tot_1995
	local max = `r(max)' +0.1
	local min = `r(min)' -0.1
	_pctile stock_tot_1995, p(25 50 75 95)
	egen bin = cut(stock_tot_1995) if year == 1995, at(`min',`r(r1)',`r(r2)',`r(r3)',`r(r4)',`max') icodes
	bys lse_id : egen bins = min(bin) 
	egen yearbins = group(year bins) if year <= 2009
	drop stock_tot bin bins

	sort lse_id year
	egen yearctry = group(year country_shr_1995) if year <= 2009
	egen yearindustry = group(year industry)

	*********
	estimates clear

	*1) F + IY + BinYear
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id) absorb(lse_id yearbins yearindustry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local by "\yes"
	estadd local cy "\no"
	estadd local has_stockspill "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col_1

	*2) VAEMP | F + IY + BinYear
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearbins yearindustry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local by "\yes"
	estadd local cy "\no"
	estadd local has_stockspill "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col_2

	*3) GDPPC | F + IY + BinYear
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP GDPPC stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearbins yearindustry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local by "\yes"
	estadd local cy "\no"
	estadd local has_stockspill "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col_3

	*4) F + IY + CY + BinYear
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearctry yearbins yearindustry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local by "\yes"
	estadd local cy "\yes"
	estadd local has_stockspill "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col_4

	*5) VAEMP | F + IY + CY + BinYear
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearctry yearbins yearindustry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local by "\yes"
	estadd local cy "\yes"
	estadd local has_stockspill "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col_5

	*6) GDPPC | F + IY + CY + BinYear
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP GDPPC stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearctry yearbins yearindustry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local by "\yes"
	estadd local cy "\yes"
	estadd local has_stockspill "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col_6	

	drop LSW HSW GDPGAP GDPPC VAEMP

	clonevar GDPPC_foreign = gdppcMPm_shr4_foreign_1995_a
	clonevar GDPPC_home = gdppcMPm_shr4_home_1995_a	
	clonevar VAEMP_foreign = vaempMPm_shr4_foreign_1995_a
	clonevar VAEMP_home = vaempMPm_shr4_home_1995_a
	clonevar GDPgap_foreign = lngdpgap_shr_foreign_1995_a
	clonevar GDPgap_home = lngdpgap_shr_home_1995_a
	clonevar LSW_home = lswMPm_shr4_home_1995_a
	clonevar LSW_foreign = lswMPm_shr4_foreign_1995_a
	clonevar HSW_home = hswMPm_shr4_home_1995_a
	clonevar HSW_foreign = hswMPm_shr4_foreign_1995_a

	*7) F + IY + CY + BinYear | foreign
	ppmlhdfe F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPgap_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearctry yearbins yearindustry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local by "\yes"
	estadd local cy "\yes"
	estadd local has_stockspill "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col_7

	*8) VAEMP | F + IY + CY + BinYear | foreign
	ppmlhdfe F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPgap_foreign VAEMP_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearctry yearbins yearindustry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local by "\yes"
	estadd local cy "\yes"
	estadd local has_stockspill "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col_8

	*9) GDPPC | F + IY + CY + BinYear | foreign
	ppmlhdfe F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPgap_foreign GDPPC_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearctry yearbins yearindustry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local by "\yes"
	estadd local cy "\yes"
	estadd local has_stockspill "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col_9	

	drop LSW_foreign HSW_foreign GDPgap_foreign GDPPC_foreign VAEMP_foreign
	drop spilloversown spilloversownzero spilloversother spilloversotherzero
	drop stockown stockownzero stockother stockotherzero

	setlabels
	dhoztab * using ${tab_dir}/appendix/Table_A36_firmsize.tex,  b(%10.2f) se(%10.2f) nogaps replace numbers nonotes nolines nomtitles sfmt(a1) scalars("has_stockspill \noalign{\medskip} Stocks and spillovers" "f Firm fixed effects" "iy Industry \stimes year fixed effects" "by Bin \stimes year fixed effects" "cy Country \stimes year fixed effects" "obs \noalign{\medskip} Observations"  "firms Number of firms") label mgroups("Domestic and foreign" "Foreign", pattern(1 0 0 0 0 0 1 0 0) span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) drop(*zero _cons spill* stock*) noobs rename(LSW_foreign LSW HSW_foreign HSW GDPgap_foreign GDPGAP VAEMP_foreign VAEMP GDPPC_foreign GDPPC) ///
		depvar("Auto95") ///
		notes("This table controls for the size of the firms. Firms are classified into five bins by the stock of total patents in 1995 with 25th, 50th, 75th, and 95th percentiles as four thresholds. All columns include firm, industry-year and bin-year fixed effects. Columns  4--9 add country-year fixed effects. In Columns 7--9, the macroeconomic variables are the normalized foreign variables as defined in the text. Standard errors are clustered at the firm-level and reported in parentheses.")

	*again, brackets do work, just dispalyed wrongly
}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close tabl